package com.lzw.d25bingso;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lzw.d25bingso.adapter.CompanyAdapter;
import com.lzw.d25bingso.bean.Company;
import com.lzw.d25bingso.titleview.TitleView;
import com.lzw.d25bingso.tools.Config;
import com.lzw.d25bingso.tools.HttpUtils;
import com.lzw.d25bingso.tools.PaseJson;

import java.util.ArrayList;
import java.util.List;

public class CompanyActiity extends AppCompatActivity {

    private ListView lv;
    private TitleView titleView;
    private List<Company> total;
    private CompanyAdapter adapter;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_actiity);
        titleView = (TitleView) findViewById(R.id.title_view_id);
        lv = (ListView) findViewById(R.id.com_lv);
        Intent intent = getIntent();
      final   String id = intent.getStringExtra("id");
        String name = intent.getStringExtra("name");
        titleView.setTextView(name);
        titleView.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        total = new ArrayList<>();
        adapter = new CompanyAdapter(this,total);
        lv.setAdapter(adapter);
        new Thread(){
            @Override
            public void run() {
                super.run();
                String jsonStr = HttpUtils.getStringResult(Config.COMPANY_PATH + id);
                final List<Company> data= PaseJson.paseCompany(jsonStr);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        total.addAll(data);
                        adapter.notifyDataSetChanged();
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(CompanyActiity.this,DetailActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("company",data.get(position));

                                    intent.putExtra("data",bundle);
                                    startActivity(intent);
                            }
                        });
                    }
                });
            }
        }.start();

    }
}
